package com.wsx.seven.a;

import java.util.Arrays;

public class Main0005 {

    public static void main(String[] args) {
        int[] array = new int[]{1, 6, 3, 9, 5, 10, 7, 8};
        qsSort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void qsSort(int[] array) {
        coreSort(array, 0, array.length - 1);
    }

    private static void coreSort(int[] array, int low, int high) {
        int start = low;
        int end = high;
        int pilot = array[low];
        while (start < end) {
            while (start < end && array[end] >= pilot) {
                end--;
            }
            if (array[end] <= pilot) {
                swap(array, start, end);
            }
            while (start < end && array[start] <= pilot) {
                start++;
            }
            if (array[start] >= pilot) {
                swap(array, start, end);
            }
        }
        if (start > low) {
            coreSort(array, low, start - 1);
        }
        if (end < high) {
            coreSort(array, end + 1, high);
        }

    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
